import Page
from Page import Launcher
from Base.Base import Base
from selenium.webdriver.common.by import By


class LauncherAppListPage(Base):
    """
    桌面列表页面
    """

    def __init__(self, driver):
        Base.__init__(self, driver)

        # 列表应用名称, 使用时需传入对应的应用名
        self.list_appname_text = (By.XPATH, Launcher.list_appname_text.format(""))

    def click_appname(self, appname):
        """
        点击指定名称的应用

        :param appname: 应用名称
        :return:
        """
        self.list_appname_text = (By.XPATH, Launcher.list_appname_text.format(appname))

        if self.is_display_appname(appname):
            self.click_element(self.list_appname_text)
        else:
            self.swipe_list(direction=Page.DIRECTION_LEFT)
            self.click_element(self.list_appname_text)

    def is_display_appname(self, appname):
        """
        是否展示指定名称的应用

        :param appname: 应用名称
        :return:
        """
        self.list_appname_text = (By.XPATH, Launcher.list_appname_text.format(appname))
        return self.is_display(self.list_appname_text)

    def swipe_list(self, direction=Page.DIRECTION_LEFT):
        """
        根据用户输入的方向, 滑动列表

        :param direction:
        :return:
        """
        self.swipe_o(location=(200, 1100, 400, 400), direction=direction)
